from django.db.models import Manager
from django.shortcuts import render, get_object_or_404

from django.views.generic import ListView, TemplateView, DetailView

from main.models import Article, Category
from django.core.paginator import Paginator,Page
# Create your views here.
class ArticleListView(ListView):
    # model = Article
    paginate_by = 1
    def get_queryset(self):
        c_id = self.request.GET.get('category')
        if c_id and c_id != 'all':
            cat = get_object_or_404(Category, id=c_id)
            queryset = cat.article_set.all()
        else:
            queryset = Article.objects.all()
        return queryset

    def get_context_data(self, **kwargs):
        kwargs.update({
            'category': self.request.GET.get('category') or 'all',
        })
        return super().get_context_data(**kwargs)

class ArticleDetailView(DetailView):
    queryset = Article.objects.all()



class IndexView(TemplateView):
    template_name = "main/index.html"
    extra_context = {
        "category": Category.objects.filter(parent__isnull=True),
    }


